# -*- coding: utf-8 -*-
import xbmc, xbmcgui, subprocess, os, time, sys, urllib, re
import xbmcplugin, xbmcaddon,time

# Shared resources
#BASE_RESOURCE_PATH = os.path.join( os.getcwd(), "resources" )
#sys.path.append( os.path.join( BASE_RESOURCE_PATH, "lib" ) )


__scriptname__ = "蓝光播放"
__scriptID__      = "plugin.playbluraydisk"
__author__ = "simon shu"
__url__ = ""
__credits__ = ""
__version__ = "0.1.1"
__addon__ = xbmcaddon.Addon(__scriptID__)

#__language__ = __addon__.getLocalizedString
#_ = sys.modules[ "__main__" ].__language__
def main():

	hasmedia=os.popen("udisks --show-info /dev/sr0 |grep 'has media'").read()[31]=='1'

	#检测是否有光盘内容
        times=10
        while (not hasmedia) and (times>0):
           times=times-1
           os.system("udisks --poll-for-media /dev/sr0")
           hasmedia=os.popen("udisks --show-info /dev/sr0 |grep 'has media'").read()[31]=='1'
           time.sleep(1)

        #无光盘
        if not hasmedia:
	   dialog = xbmcgui.Dialog()
	   ok = dialog.ok(__scriptname__, '无法播放：请插入光盘后再试')
	   return

        ismounted=os.popen("udisks --show-info /dev/sr0 |grep 'is mounted'").read()[31]=='1'
	#已经加载
	if ismounted:
	    mountpath=os.popen("udisks --show-info /dev/sr0 |grep 'mount paths'").read()[27:-1]
	else:
	    ret=os.popen("udisks --mount /dev/sr0").read()
	    if ret[:7]=="Mounted":
		mountpath=ret[47:].strip()
	    else:
		dialog = xbmcgui.Dialog()
		ok = dialog.ok(__scriptname__, '无法播放：无法读取光盘')
		return

	if not os.path.exists(mountpath):
	   dialog = xbmcgui.Dialog()
	   ok = dialog.ok(__scriptname__, '无法播放：无效的光盘格式')
	   return


	if os.path.exists(mountpath+"/BDMV/index.bdmv"):
	    xbmc.Player().play(mountpath+"/BDMV/index.bdmv")
	else:
	    xbmc.executebuiltin('PlayDVD')
	    #xbmc.Player().play("/var/media/cdrom/")

if ( __name__ == "__main__" ):
    main()

